package com.ruima.study.jdk21.jdk21.demo1;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <p>
 * <p></p>
 * <p>
 * <p>Copyright (c) 2024/11/14 15:30, 科大讯飞 All Rights Reserve</p>
 *
 * @author ruima6
 * @version [V1.0]
 */
public class ScopeValueDemo2 {
    private static final ScopedValue<String> GIFT = ScopedValue.newInstance();

    public static void main(String[] args) {
        ScopeValueDemo2 t = new ScopeValueDemo2();

        ExecutorService pool = Executors.newCachedThreadPool();

        for (int i = 0; i < 10; i++) {
            pool.submit(()->{
                t.giveGift();
            });


        }

        pool.shutdown();
    }

    //向ScopedValue中添加当前线程的名字
    public void giveGift() {
        ScopedValue.where(GIFT, Thread.currentThread().getName()).run(() -> receiveGift());
    }

    public void receiveGift() {
        System.out.println(GIFT.get());
    }
}
